/**
 * Created by Administrator on 2017/5/17.
 */

$(function(){
    index2();
});
var index2fiveInterval;
function index2(){
    clearInterval(index2fiveInterval);
    sailCount();
    dpSailChart1();
    salesCurve();
    allAly();
    realTimeOrder();
    kdj();
    averagePrice();
    index2five();
    index2fiveInterval=setInterval(index2five,300000);
}

/****************第3标签方法开始*************************/

//5分钟定时任务
function index2five() {
    realTimeOrder();
}
function sailCount() {
    var southValue=0;
    var northValue=0;
    $.get( "../../jxc/jxcsaleorder/sailCount?type="+zytype+"&time="+zytime,function(r){

        $("#totalz").html((r.southCount+r.northCount)+"元");
        $("#stotalz").html(r.southCount+"元");
        $("#ntotalz").html(r.northCount+"元");
    })
}
//单品销量排行
function dpSailChart1() {
    var detailName=[];
    var detaiAmount=[];
    $.get( "../../jxc/jxcselldetails/detailsbytime?type="+zytype+"&time="+zytime,function(r){

        for (var i = r.detailsRanking.length-1; i >=0; i--) {
            detailName.push(r.detailsRanking[i].yvalue);
            detaiAmount.push(r.detailsRanking[i].xvalue);
        }
        var myChart = echarts.init($("#paihang1")[0]);
        var option = {
            tooltip : {
                trigger: 'axis',
                formatter: function(params) { //自定义函数修改折线图给数据加单位
                    var str = '';//声明一个变量用来存储数据
                    str += '<div>'+ params[0].name +'</div>'; //显示首行的函数
                    for(var i=0; i<params.length; i++) { //图显示的数据较多时需要循环数据，这样才可以成功的给多条数据添加单位
                        str += '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:'+params[i].color+';"></span>'+ params[i].seriesName +'</span> : <span>'+ params[i].data+'份' +'</br>';
                    }
                    return str;
                },
                axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                    type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            grid: {
                x: 90,
                y:30,
                x2:32,
                y2:40,
                borderWidth: 0
            },
            xAxis:  {
                type: 'value',
                name:'销量/个',
                x:'180',
                splitLine: {
                    show: false
                },
                axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#a4a7ab'
                    }
                }
            },
            yAxis: {
                type: 'category',
                name:'商品名称',
                data: detailName,
                splitLine: {
                    show: false
                },
                axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#a4a7ab'
                    }
                }
            },
            series: [
                {
                    name: '销售数量',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'insideRight'
                        }
                    },
                    data: detaiAmount,
                    itemStyle: {
                        normal: {
                            color:"#ff7d0a"
                        }
                    }
                }
            ]
        };
        myChart.setOption(option);
        window.addEventListener("resize",function(){
            myChart.resize();
        });
    })
}
//销售趋势
function salesCurve() {
    var xname=null;
    if(zytype == "0"){
        xname="小时";
    }else if(zytype == "1"){
        xname="日期";
    }
    var name=[];
    var sousale=[];
    var norsale=[];
    var smakmax=null;//南区最大值
    var smaksmall=null;//南区最小值
    var smakmaxx=null;//南区最大值x轴
    var smaksmallx=null;//南区最小值x轴
    var smakmaxy=null;//南区最大值y轴
    var smaksmally=null;//南区最小值y轴
    var nmakmax=null;//北区最大值
    var nmaksmall=null;//北区最小值
    var nmakmaxx=null;//北区最大值x轴
    var nmaksmallx=null;//北区最小值x轴
    var nmakmaxy=null;//北区最大值y轴
    var nmaksmally=null;//北区最小值y轴
    $.get( "../../jxc/jxcsaleorder/salebytime?type="+zytype+"&time="+zytime,function(r){
         name=r.southsale[0];
         //sousale=r.southsale[1];//南区集合
        for (let i = 0; i <r.southsale[1].length; i++) {
            sousale.push(r.southsale[1][i].toFixed(2));
        }
        // norsale=r.northsale[1];//北区集合
        for (let i = 0; i <r.northsale[1].length; i++) {
            norsale.push(r.northsale[1][i].toFixed(2));
        }
        smakmax=sousale[0];
        smaksmall=sousale[0];
        smakmaxx=name[0];
        smaksmallx=name[0];
        smakmaxy=sousale[0];
        smaksmally=sousale[0];

        nmakmax=norsale[0];
        nmaksmall=norsale[0];
        nmakmaxx=name[0];
        nmakmaxy=norsale[0];
        nmaksmallx=name[0];
        nmaksmally=norsale[0];
        for (var i = 0; i <r.southsale[1].length ; i++) {
            if(r.southsale[1][i] >=smakmax){
                smakmaxx=r.southsale[0][i];
                smakmax=r.southsale[1][i].toFixed(2);
                smakmaxy=r.southsale[1][i].toFixed(2);
            }else if(r.southsale[1][i] <=smaksmall){
                smaksmallx=r.southsale[0][i];
                smaksmall=r.southsale[1][i].toFixed(2);
                smaksmally=r.southsale[1][i].toFixed(2);
            }
        }
        for (var i = 0; i <r.northsale[1].length ; i++) {
            if(r.northsale[1][i] >= nmakmax){
                nmakmaxx=r.northsale[0][i];
                nmakmax=r.northsale[1][i].toFixed(2);
                nmakmaxy=r.northsale[1][i].toFixed(2);
            }else if(r.northsale[1][i] <= nmaksmall){
                nmaksmallx=r.northsale[0][i];
                nmaksmall=r.northsale[1][i].toFixed(2);
                nmaksmally=r.northsale[1][i].toFixed(2);
            }
        }
        var myChart = echarts.init($("#salesCurve")[0]);
        option = {
            title : {
                text: '',
                textStyle:{
                    color:"#e9ebee"
                },
                x:'center'
            },
            tooltip : {
                trigger: 'axis',
                formatter: function(params) { //自定义函数修改折线图给数据加单位
                    var str = '';//声明一个变量用来存储数据
                    str += '<div>'+ params[0].name +'</div>'; //显示首行的函数
                    for(var i=0; i<params.length; i++) { //图显示的数据较多时需要循环数据，这样才可以成功的给多条数据添加单位
                        str += '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:'+params[i].color+';"></span>'+ params[i].seriesName +'</span> : <span>'+ params[i].data+'元' +'</br>';
                    }
                    return str;
                }
            },
            legend: {
                orient : 'vertical',
                x : 'left',
                data:['南区','北区'],
                textStyle:{
                    color:"#e9ebee"

                },
            },

            calculable : false,
            xAxis : [
                {
                    type : 'category',
                    name:xname,
                    splitLine:{show: false},
                    axisLabel : {
                        formatter: '{value} ',
                        textStyle: {
                            color: '#a4a7ab',
                            align: 'right'
                        }
                    },
                    data : name,
                }
            ],
            yAxis : [
                {
                    type : 'value',
                    name:'销售额/元',
                    splitLine:{show: false},
                    axisLabel : {
                        formatter: '{value} ',
                        textStyle: {
                            color: '#a4a7ab',
                            align: 'right'
                        }
                    }
                }
            ],
            grid:{
                borderWidth:0
            },
            series : [
                {
                    name:'南区',
                    type:'bar',
                    data:sousale,
                    itemStyle: {
                        normal: {
                            color: '#2481ff'
                        }
                    },
                    markPoint : {
                        data : [
                            {name : '最高', value : smakmax, xAxis: smakmaxx, yAxis: smakmaxy, symbolSize:18},
                            {name : '最低', value : smaksmall, xAxis: smaksmallx, yAxis: smaksmally}
                        ]
                    },
                    markLine : {
                        data : [
                            {type : 'average', name: '平均值'}
                        ]
                    }
                },
                {
                    name:'北区',
                    type:'bar',
                    data:norsale,
                    itemStyle: {
                        normal: {
                            color: '#1afffd'
                        }
                    },

                    markPoint : {
                        data : [
                            {name : '最高', value : nmakmax, xAxis: nmakmaxx, yAxis: nmakmaxy, symbolSize:18},
                            {name : '最低', value : nmaksmall, xAxis: nmaksmallx, yAxis: nmaksmally}
                        ]
                    },
                    markLine : {
                        data : [
                            {type : 'average', name : '平均值'}
                        ]
                    }
                }
            ]
        };
        myChart.setOption(option);
        window.addEventListener("resize",function(){
            myChart.resize();
        });
    })
}

function allAly() { /*呵呵*/
    var southValue = [];
    var northValue = [];
    var clor = ['#1afffd', '#2e7cff', '#ffcb89', '#005ea1', '#0ad5ff','#990099','#FF0000','#009900','#FF00FF','#FFC0CB','#B0C4DE','#C0C0C0','#8B4513','#FFE4B5','#808000','#7CFC00','#40E0D0','#BA55D3','#F0FFFF','#E6E6FA','#5F9EA0'];
    $.get("../../jxc/jxcsaleorder/orderbytime?type="+zytype+"&time="+zytime, function (r) {
        var southList=r.southvolume;
        var northList=r.northvolume;
        for (let i = 0; i < southList.length; i++) {
            southValue.push({
                value: (southList[i].totalAmount).toFixed(2), name: southList[i].shopName, itemStyle: {
                    /* normal: {
                         color: clor[i]
                     }*/
                }
            });
        }
        for (let i = 0; i < northList.length; i++) {
            northValue.push({
                value: (northList[i].totalAmount).toFixed(2), name: northList[i].shopName, itemStyle: {
                    /* normal: {
                         color: clor[i]
                     }*/
                }
            });
        }
        var myChart = echarts.init($("#allAly01")[0]);
        option = {
            tooltip : {
                trigger: 'item',
                formatter: "{a}<br/>{b}:{c}"+"元"+"({d}%)",
                position:[130,230]
            },
            title: {
                text: '南区',
                textStyle: {
                    color: "#e9ebee"

                },
                x: 'center'

            },
            calculable: false,
            series: [
                {
                    name: '销售额',
                    type: 'pie',
                    radius: '55%',
                    center: ['50%', '60%'],
                    data: southValue
                }
            ]
        };
        myChart.setOption(option);
        window.addEventListener("resize",function(){
            myChart.resize();
        });
        var myChart1 = echarts.init($("#allAly02")[0]);
        option = {
            tooltip : {
                trigger: 'item',
                formatter: "{a}<br/>{b}:{c}"+"元"+"({d}%)",
                position:[130,230]
            },
            title: {
                text: '北区',
                textStyle: {
                    color: "#e9ebee"

                },
                x: 'center'

            },
            calculable: false,
            series: [
                {
                    name: '销售额',
                    type: 'pie',
                    radius: '55%',
                    center: ['50%', '60%'],
                    data: northValue
                }
            ]
        };
        myChart1.setOption(option);
        window.addEventListener("resize",function(){
            myChart1.resize();
        });
    });
}
function realTimeOrder() {
    var info="";
    $.get( "../../jxc/jxcsaleorder/realtimeorder",function(r){
        var list=r.realtimeorder;
        var infoId
        for (var i = 0; i <list.length ; i++) {
            $("#realtimed").empty()
            if(i==0){
                infoId=list[i].sellMasterCode;
            }
            info +="<div class='message_scroll1'><div class='scroll_top'>" +
                "<span class='scroll_level' style='width: 25%'>"+list[i].sellMasterDate+"</span>" +
                "<span class='scroll_level' style='width: 10%'>"+list[i].name+"</span>" +
                "<span class='scroll_level' style='width: 20%'>"+list[i].shopName+"</span>" +
                "<span class='scroll_level' style='width: 15%'>数量:"+list[i].totalCount+"</span>" +
                "<span class='scroll_level' style='width: 18%'>总金额:"+list[i].totalAmount+"元</span>" +
                "<span id="+list[i].sellMasterCode+" onclick='querydetail(this)' class='scroll_level' style='width: 5%'><a>详情</a></span>" +
                /* "<span class='scroll_level' style='float: right'><a style='color: #e9a832;font-size: 12px'>银监回放</a></span>" +*/
                "</div></div>";


        }
        $("#realtimed").append(info)
        $("#"+infoId).click();
    })
}

//实时订单详情点击事件
function querydetail(data){
    var orderCode=$(data).attr('id');
    $(".order").removeClass("order");
    $(data).parent().addClass("order");
    var info="<tr><td class='font04'>商品名称</td><td class='font04'>数量</td><td class='font04'>单价</td><td class='font04'>优惠</td><td class='font04'>总价</td></tr>>";
    $.get( "../../jxc/jxcselldetails/querydetail?id="+orderCode,function(r){

        $("#detaillist").empty();
        var detaillist=r.querydetail;
        for (var i = 0; i <detaillist.length ; i++) {
            info +="<tr><td>"+detaillist[i].commodityName+"</td><td>"+detaillist[i].sellCount+"</td><td>"+detaillist[i].price+"元</td><td>"+detaillist[i].discount+"元</td><td>"+detaillist[i].sellAmount+"元</td></tr>";
        }
        $("#detaillist").append(info);
    })
}
//客流销售额趋势
function kdj() {
    var xname=null;
    if(zytype == "0"){
        xname="小时";
    }else if(zytype == "1"){
        xname="日期";
    }
    var timeArray=[];
    var salelist=[];
    var rlllist;
    $.get( "../../jxc/jxcsaleorder/salerll?type="+zytype+"&time="+zytime,function(r){
        var maps=r.salecllrlllist;
        timeArray=maps["销售额"][0];
        //salelist=maps["销售额"][1];
        for (let i = 0; i <maps["销售额"][1].length ; i++) {
            salelist.push(maps["销售额"][1][i].toFixed(2));
        }
        rlllist=maps["客流量"];
        /*for (var key in maps) {
            thname.push(key);
            if(key =="销售额"){
                for (var i = maps[key].length-1; i >=0 ; i--) {
                    timeArray.push(maps[key][i].name);
                    salelist.push(maps[key][i].yvalue)
                }
            }else if(key =="客流量"){
                for (var i = maps[key].length-1; i >=0 ; i--) {
                    rlllist.push(maps[key][i].yvalue)
                }
            }
        }*/
        var myChart = echarts.init(document.getElementById('salesCurve1'));
        var option = {
            tooltip : {
                trigger: 'axis',
                formatter: function(params) { //自定义函数修改折线图给数据加单位
                    var str = '';//声明一个变量用来存储数据
                    str += '<div>'+ params[0].name +'</div>'; //显示首行的函数
                    str += '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:'+params[0].color+';"></span>'+ params[0].seriesName +'</span> : <span>'+ params[0].data+'元' +'</br>';
                    str += '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:'+params[1].color+';"></span>'+ params[1].seriesName +'</span> : <span>'+ params[1].data+'人次' +'</br>';
                    return str;
                }
            },
            grid: {
                x: 46,
                y:30,
                x2:32,
                y2:60,
                borderWidth: 0
            },
            calculable : true,
            legend: {
                x: 'center',
                data: ["销售金额","客流量"],
                textStyle:{
                    color:"#e9ebee"
                }
            },
            xAxis : [
                {
                    type : 'category',
                    name:xname,
                    boundaryGap : false,
                    data :timeArray,
                    splitLine: {
                        show: false
                    },
                    axisLabel: {
                        show: true,
                        textStyle: {
                            color: '#a4a7ab'
                        }
                    }
                }
            ],
            yAxis : [
                {
                    type: 'value',
                    name: '销售金额/元',
                    splitLine: {
                        show: false
                    },
                    interval: 50,
                    axisLabel: {
                        show: true,
                        textStyle: {
                            color: '#a4a7ab'
                        },
                    }
                },
                {
                    type: 'value',
                    name: '流量/次',
                    splitLine: {
                        show:false
                    },
                    interval: 5,
                    axisLabel: {
                        show: true,
                        textStyle: {
                            color: '#a4a7ab'
                        },
                    }
                }
            ],
            series : [
                {
                    name:'销售金额',
                    type:'line',
                    data:salelist,
                    smooth: true,
                    itemStyle: {
                        normal: {
                            color:"#e15828"
                        }
                    }
                },
                {
                    name:'客流量',
                    type:'line',
                    yAxisIndex: 1,
                    data:rlllist,
                    smooth: true,
                    itemStyle: {
                        normal: {
                            color:"#e9a832"
                        }
                    }
                }
            ]
        };
        myChart.setOption(option);
        window.addEventListener("resize",function(){
            myChart.resize();
        });
    })
}

//客单价
function averagePrice(){
    var xname=null;
    if(zytype == "0"){
        xname="小时";
    }else if(zytype == "1"){
        xname="日期";
    }
     $.get( "../../jxc/jxcsaleorder/averagePrice?type="+zytype+"&time="+zytime,function(r){
        var list=r.averagePricelist;
        var myChart = echarts.init(document.getElementById('kdj'));
        var option = {
        tooltip : {
            trigger: 'axis',
            formatter: function(params) { //自定义函数修改折线图给数据加单位
                var str = '';//声明一个变量用来存储数据
                str += '<div>'+ params[0].name +'</div>'; //显示首行的函数
                for(var i=0; i<params.length; i++) { //图显示的数据较多时需要循环数据，这样才可以成功的给多条数据添加单位
                    str += '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:'+params[i].color+';"></span>'+ params[i].seriesName +'</span> : <span>'+ params[i].data+'元' +'</br>';
                }
                return str;
            }
        },
        grid: {
            x: 46,
            y:30,
            x2:20,
            borderWidth: 0
        },
        calculable : true,
        xAxis : [
            {
                type : 'category',
                name:xname,
                boundaryGap : false,
                data :r.averagePricelist[0],
                splitLine: {
                    show: false
                },
                axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#a4a7ab'
                    }
                }
            }
        ],
        yAxis : [
            {
                type: 'value',
                name: '客单价/元',
                splitLine: {
                    show: false
                },
                interval: 50,
                axisLabel: {
                    show: true,
                    textStyle: {
                        color: '#a4a7ab'
                    },
                }
            }
        ],
        series : [
            {
                name:'客单价',
                type:'line',
                data:r.averagePricelist[1],
                smooth: true,
                itemStyle: {
                    normal: {
                        color:"#e15828"
                    }
                }
            }
        ]
    };
    myChart.setOption(option);
    window.addEventListener("resize",function(){
        myChart.resize();
    });

    })

}
//时间选择
var zytype="0";
var zytime="";
function choicetime(data){
    if(data =="0"){
        zytype="0";
        $("#choiceday").css("display","inline-block");
        $("#choiceday").val("");
        $("#choicemonth").val("");
        zytime ="";
        $("#thisDay").css("background-color","#3366FF");
        $('#thisMonth').css("background-color","");
        $("#choicemonth").css("display","none");
        index2();
    }else if(data =="1"){
        zytype="1";
        $("#choicemonth").css("display","inline-block");
        $("#choiceday").val("");
        $("#choicemonth").val("");
        zytime ="";
        $("#thisMonth").css("background-color","#3366FF");
        $('#thisDay').css("background-color","");
        $("#choiceday").css("display","none");
        index2();
    }
}
//选择时间后 点击查询事件
function query(){
    //获取input框的值
    if(zytype =="0"){
        zytime =$("#choiceday").val();
    }else if(zytype=="1"){
        zytime =$("#choicemonth").val();
    }
    index2();
}
